package com.bkhc.common.api;

import com.bkhc.common.annotation.ApiOperation;
import com.bkhc.common.annotation.ApiPackage;
import com.bkhc.common.annotation.AuditLog;
import com.bkhc.common.entity.User;
import com.bkhc.common.entity.UserRole;
import com.bkhc.common.enu.OperationEnum;
import com.bkhc.common.permissions.bean.UserPermission;
import com.bkhc.common.service.UserService;

import com.bkhc.common.util.CommonUtil;
import com.bkhc.common.util.response.ResultCode;
import com.bkhc.common.util.response.ResultJson;
import com.bkhc.common.vo.UserBean;
import com.bkhc.common.vo.UserVo;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author pcq
 * @date 2021/6/20 17:57
 */
@RestController
@RequestMapping("/user")
@Api("后台管理服务接口")
@ApiPackage(code="userService",name="后台用户服务接口",description = "后台管理员相关维护操作接口")
public class UserController extends CommonController {

    @Autowired
    private UserService userSerivce;

    @ApiOperation(code="queryUser",name="查询后台用户",description = "查询后台所有用户")
    @AuditLog(object="User",operation = "查询用户",type = OperationEnum.query)
    @RequestMapping(path = "/query/{pageNum}/{pageSize}",method = RequestMethod.POST)
    public ResultJson<?> getAllUser(@RequestBody(required=false) UserVo userVo, @PathVariable(name="pageNum") int pageIndex, @PathVariable(name="pageSize") int pageSize) {
        ResultJson<?> res = userSerivce.queryUser(userVo, pageIndex, pageSize);
        return res;
    }

    @ApiOperation(code="queryUserByUserId",name="通过userId查询用户",description = "通过userId查询用户")
    @AuditLog(object="User",operation = "通过userId查询用户",type = OperationEnum.query)
    @RequestMapping(path = "/query/{userId}",method = RequestMethod.POST)
    public ResultJson<?> queryUserByUserId(@PathVariable String userId) {
        ResultJson<?> res = userSerivce.queryUserByUserId(userId);
        return res;
    }

    @ApiOperation(code="queryUserByRoleCode",name="通过角色编码查询用户",description = "通过角色编码查询用户")
    @AuditLog(object="User",operation = "通过角色编码查询用户",type = OperationEnum.query)
    @RequestMapping(path = "/query/roleCode/{roleCode}",method = RequestMethod.POST)
    public ResultJson<?> queryUserByRoleCode(@PathVariable String roleCode) {
        ResultJson<?> res = userSerivce.queryUserByRoleCode(roleCode);
        return res;
    }

    @ApiOperation(code="addUser",name="增加后台",description = "增加后台用户")
    @AuditLog(object="User",operation = "增加用户",type = OperationEnum.add)
    @RequestMapping(path = "/add",method = RequestMethod.POST)
    public ResultJson<?> addUser(@RequestBody(required=false) User user) {
        ResultJson<?> res = userSerivce.addUser(user);
        return res;
    }

    @ApiOperation(code="updateUser",name="后台",description = "修改后台用户")
    @AuditLog(object="User",operation = "修改用户",type = OperationEnum.update)
    @RequestMapping(path = "/update",method = RequestMethod.POST)
    public ResultJson<?> updateUser(@RequestBody(required=false) User user) {
        ResultJson<?> res = userSerivce.updateUser(user);
        return res;
    }

    @ApiOperation(code="saveOrupdateUserRole",name="分配角色",description = "分配角色")
    @AuditLog(object="User",operation = "分配角色",type = OperationEnum.update)
    @RequestMapping(path = "/role/saveOrupdate",method = RequestMethod.POST)
    public ResultJson<?> saveOrupdateUserRole(@RequestBody(required = true) UserRole userRole) {
        ResultJson<?> res = userSerivce.saveOrupdateUserRole(userRole);
        return res;
    }


    @ApiOperation(code="deleteUser",name="后台",description = "删除后台用户")
    @AuditLog(object="User",operation = "删除用户",type = OperationEnum.delete)
    @RequestMapping(path = "/del",method = RequestMethod.POST)
    public ResultJson<?> deleteUser(@RequestBody(required=false) User user) {
        ResultJson<?> res = userSerivce.delete(user);
        return res;
    }

    @ApiOperation(code="queryUserRole",name="查询用户角色",description = "查询后台用户角色")
    @AuditLog(object="User",operation = "查询用户",type = OperationEnum.query)
    @RequestMapping(path = "/queryUserRole/{userId}",method = RequestMethod.POST)
    public ResultJson<?> getUserRole(@PathVariable(name="userId") String userId,@RequestParam(name="status") String status) {
        ResultJson<?> res = null;
        if(StringUtils.isEmpty(status)){
            res=userSerivce.getUserRole(userId);
        }else {
            res= userSerivce.getUserRole(userId, 1);
        }
        return res;
    }

    @ApiOperation(code="queryUserRoleDetail",name="查询用户角色详情",description = "查询后台用户角色详情")
    @AuditLog(object="UserRole",operation = "查询用户",type = OperationEnum.query)
    @RequestMapping(path = "/queryUserRoleDetail/{userId}",method = RequestMethod.POST)
    public ResultJson<?> queryUserRoleDetail(@PathVariable(name="userId") String userId) {
        ResultJson<?> res = userSerivce.queryUserRoleDetail(userId);
        return res;
    }

    @ApiOperation(code="queryUserPermission",name="查询用户权限详细列表",description = "查询用户权限详细列表")
    @AuditLog(object="UserPermission",operation = "查询用户权限详细列表",type = OperationEnum.query)
    @RequestMapping(path = "/queryUserPermission",method = RequestMethod.GET)
    public ResultJson<?> queryUserPermission(@RequestParam(name="userId") String userId) {
        UserPermission userPermission = userSerivce.queryUserPermission(userId);
        ResultJson<UserPermission> resultJson = ResultJson.success();
        resultJson.setData(userPermission);
        return resultJson;
    }

    @ApiOperation(code="updateUserRoleDetail",name="更新用户角色详情",description = "更新后台用户角色详情")
    @AuditLog(object="UserRole",operation = "更新用户角色详情",type = OperationEnum.query)
    @RequestMapping(path = "/updateUserRoleDetail/{userId}",method = RequestMethod.POST)
    public ResultJson<?> updateUserRoleDetail(@RequestBody(required = false) UserRole userRole) {
        ResultJson<?> res = userSerivce.updateUserRoleDetail(userRole);
        return res;
    }

    @ApiOperation(code="getUserByUserId",name="查询UMC后台用户",description = "查询UMC后台用户")
    @AuditLog(object="User",operation = "查询UMC后台用户",type = OperationEnum.query)
    @RequestMapping(path = "/umc/getUserByUserId",method = RequestMethod.POST)
    public ResultJson<User> getUserByUserId(@RequestParam(name="userId") String userId) {
        User  user= userSerivce.getUserByUserId(userId);
        ResultJson<User> resultJson = ResultJson.success();
        resultJson.setData(user);
        return resultJson;
    }

    @ApiOperation(code="getUserByUsername",name="查询UMC后台用户",description = "查询UMC后台用户")
    @AuditLog(object="User",operation = "查询UMC后台用户",type = OperationEnum.query)
    @RequestMapping(path = "/umc/getUserByUsername",method = RequestMethod.POST)
    public ResultJson<List<User>> getUserByUsername(@RequestParam(name="username") String username) {
        List<User> users= userSerivce.getUserByUsername(username);
        ResultJson<List<User>> resultJson = ResultJson.success();
        resultJson.setData(users);
        return resultJson;
    }

    @ApiOperation(code="getCurrentLoginUserInfo",name="查询UMC后台用户",description = "查询UMC后台用户",type = "common")
    @AuditLog(object="User",operation = "查询UMC后台用户",type = OperationEnum.query)
    @RequestMapping(path = "/getCurrentLoginUserInfo",method = RequestMethod.POST)
    public ResultJson<User> getCurrentLoginUserInfo() {
        UserBean u = CommonUtil.getCrruentUser();
        ResultJson<User> resultJson = userSerivce.getUserRole(u.getUserId(), 1);
        return resultJson;
    }

    @ApiOperation(code="getCurrentLoginUserPermission",name="查询UMC后台用户权限",description = "查询UMC后台用户权限",type = "common")
    @AuditLog(object="User",operation = "查询UMC后台用户权限",type = OperationEnum.query)
    @RequestMapping(path = "/getCurrentLoginUserPermission",method = RequestMethod.POST)
    public ResultJson<User> getCurrentLoginUserPermission() {
        UserBean u = CommonUtil.getCrruentUser();
        if(u==null){
            ResultJson resultJson= ResultJson.failure(ResultCode.NO_LOGIN);
        }
        UserPermission userPermission = userSerivce.queryUserPermission(u.getUserId());
        ResultJson resultJson= ResultJson.success();
        resultJson.setData(userPermission);
        return resultJson;
    }

    @ApiOperation(code="updateStatus",name="停用、启用用户",description = "停用、启用用户")
    @AuditLog(object="Goods",operation = "停用、启用用户",type = OperationEnum.add)
    @RequestMapping(path = "/updateStatus/{userId}",method = RequestMethod.POST)
    public ResultJson<?> updateStatus(@PathVariable(name="userId") String userId,@RequestParam(name="status") String status) {
        ResultJson<?> res = userSerivce.updateStatus(userId,status);
        return res;
    }
}
